package com.UdpRecieve.framework.aspect;

import com.UdpRecieve.tool.SpringUtils;
import com.UdpRecieve.domain.system.SysOperLog;
import com.UdpRecieve.framework.annotation.LogAnnotion;
import com.UdpRecieve.framework.constant.ServletConstant;
import com.UdpRecieve.framework.enums.BusinessType;
import com.UdpRecieve.framework.utils.IpUtils;
import com.UdpRecieve.framework.utils.ServletUtils;
import com.UdpRecieve.service.SysLogService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.RequestContextHolder;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

@Aspect
@Component
public class LogAspect {

    private SysLogService sysLogService = SpringUtils.getBean(SysLogService.class);

    /**
     * 切入点/织入点
     */
    @Pointcut("@annotation(com.UdpRecieve.framework.annotation.LogAnnotion)")
    public void logPointCut() {
    }

    /**
     * 处理完请求后执行
     */
    @AfterReturning(pointcut = "logPointCut()",returning = "jsonResult")
    public void doAfterReturing(JoinPoint joinPoint, Object jsonResult){

        //获得注解
        LogAnnotion logAnnotion = getAnnotationLog(joinPoint);
        if (logAnnotion == null)
        {
            return;
        }
        //获取注解内容
        String title =logAnnotion.title();
        BusinessType businessType = logAnnotion.businessType();
        //获取在线用户
        Map<String,String> onlineUserList =  ServletConstant.OnlineUserList;
        String sessionId = RequestContextHolder.getRequestAttributes().getSessionId();
        String userName = null;
        for(Map.Entry<String,String> entry:onlineUserList.entrySet()){
            if (sessionId.equals(entry.getValue())){
                userName = entry.getKey();
            }
        }
        //获取客户端ip
        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());

        //获取操作结果
        ModelMap m = (ModelMap) jsonResult;
        String result = String.valueOf(m.getAttribute("result"));

        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setIpAddress(ip);
        sysOperLog.setTitle(title);
        sysOperLog.setUserName(userName);
        sysOperLog.setResult(result.equals("-1")?"失败":"成功");
        sysOperLog.setOperaType(businessType.ordinal());
        sysOperLog.setOperaTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        int i = sysLogService.insertOperaLog(sysOperLog);
        System.out.println(i);
    }

    /**
     * 获取注解
     * @param joinPoint
     * @return
     */
    private LogAnnotion getAnnotationLog(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method!=null){
            return method.getAnnotation(LogAnnotion.class);
        }
        return null;
    }


}
